import { RouteRecordRaw,useRouter  } from 'vue-router'
import { useUserInfo } from '../stores/userInfo'
const pageTitle = (name: string): string => {
    return `pagesTitle.${name}`
}
/*
 * 静态路由
 */
const staticRoutes: Array<RouteRecordRaw> = [
    {
        // 首页
        path: '/',
        name: '/',
        component: () => import('/@/views/index.vue'),
        meta: {
            title: pageTitle('home'),
        },
    },
    {
        // 首页
        path: '/product',
        name: '/product',
        component: () => import('/@/views/product/index.vue'),
        meta: {
            title: pageTitle('产品'),
        },
    },
    {
        // 首页
        path: '/product/detail',
        name: '/product/detail',
        component: () => import('/@/views/product/detail.vue'),
        meta: {
            title: pageTitle('产品'),
        },
    },
    {
        // 登录页
        path: '/login',
        name: 'userLogin',
        component: () => import('/@/views/login.vue'),
        meta: {
            title: '用户登录',
        },
    },
    {
        path: '/:path(.*)*',
        redirect: '/404',
    },
    {
        // 404
        path: '/404',
        name: 'notFound',
        component: () => import('/@/views/common/error/404.vue'),
        meta: {
            title: pageTitle('notFound'), // 页面不存在
        },
    },
]






//用户个人中心
const merchantBaseRoute: RouteRecordRaw = {
    
    path: '/user',
    name: 'user',
    component: () => import('/@/views/user/index.vue'),
    redirect: '/user/home',
    meta: {
        title: pageTitle('user'),
    },
    children: [
        {
            // 个人中心
            path: '/user/home',
            name: 'userHome',
            component: () => import('/@/views/user/home.vue'),
            meta: {
                title: '首页',
            },
        },
        {
            // 个人中心
            path: '/user/pwd',
            name: 'pwd',
            component: () => import('/@/views/user/pwd.vue'),
            meta: {
                title: '账号密码',
            },
        },
        {
            // 个人中心
            path: '/user/balance',
            name: 'balance',
            component: () => import('/@/views/user/balance.vue'),
            meta: {
                title: '账号余额',
            },
        },
        {
            // 个人中心
            path: '/user/recharge',
            name: 'recharge',
            component: () => import('/@/views/user/recharge.vue'),
            meta: {
                title: '订单充值',
            },
        },
        {
            // 个人中心
            path: '/user/dowload',
            name: 'dowload',
            component: () => import('/@/views/user/dowload.vue'),
            meta: {
                title: '我的下载',
            },
        },
        {
            // 个人中心
            path: '/user/collect',
            name: 'collect',
            component: () => import('/@/views/user/collect.vue'),
            meta: {
                title: '我的收藏',
            },
        },
        {
            // 个人中心
            path: '/user/footprint',
            name: 'footprint',
            component: () => import('/@/views/user/footprint.vue'),
            meta: {
                title: '我的足迹',
            },
        },
        {
            // 个人中心
            path: '/user/message',
            name: 'message',
            component: () => import('/@/views/user/message.vue'),
            meta: {
                title: '消息中心',
            },
        }
    ],
    beforeEnter:(to,form,next)=>{
        const userInfo = useUserInfo() //获取用户信息
        const router = useRouter()
		if(userInfo.token){
            next()
        }else{
            router.push('/login')
        }
	}
}
staticRoutes.push(merchantBaseRoute)
export { staticRoutes  }
